home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / newtable.fr_ / newtable.fr
Text File  |  1995-07-04  |  13KB  |  439 lines

  1. VERSION 4.00
  2. Begin VB.Form frmMain 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Table Creator"
  5.    ClientHeight    =   4320
  6.    ClientLeft      =   2100
  7.    ClientTop       =   1500
  8.    ClientWidth     =   6330
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   4725
  19.    Left            =   2040
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   4320
  22.    ScaleWidth      =   6330
  23.    Top             =   1155
  24.    Width           =   6450
  25.    Begin VB.CommandButton cmdListTables 
  26.       Caption         =   "&List Tables"
  27.       Height          =   615
  28.       Left            =   4620
  29.       TabIndex        =   10
  30.       Top             =   1860
  31.       Width           =   1395
  32.    End
  33.    Begin VB.CommandButton cmdRemoveField 
  34.       Caption         =   "&Remove Field"
  35.       Height          =   615
  36.       Left            =   4620
  37.       TabIndex        =   9
  38.       Top             =   1140
  39.       Width           =   1395
  40.    End
  41.    Begin VB.ComboBox cboFieldTypes 
  42.       Height          =   300
  43.       Left            =   1800
  44.       Style           =   2  'Dropdown List
  45.       TabIndex        =   5
  46.       Top             =   1260
  47.       Width           =   2535
  48.    End
  49.    Begin VB.CommandButton cmdClose 
  50.       Cancel          =   -1  'True
  51.       Caption         =   "Close"
  52.       Height          =   615
  53.       Left            =   4620
  54.       TabIndex        =   12
  55.       Top             =   3300
  56.       Width           =   1395
  57.    End
  58.    Begin VB.CommandButton cmdCreateTable 
  59.       Caption         =   "&Create Table"
  60.       Height          =   615
  61.       Left            =   4620
  62.       TabIndex        =   11
  63.       Top             =   2580
  64.       Width           =   1395
  65.    End
  66.    Begin VB.CommandButton cmdAddField 
  67.       Caption         =   "&Add Field"
  68.       Default         =   -1  'True
  69.       Height          =   615
  70.       Left            =   4620
  71.       TabIndex        =   8
  72.       Top             =   420
  73.       Width           =   1395
  74.    End
  75.    Begin VB.ListBox lstFields 
  76.       Height          =   1815
  77.       Left            =   1380
  78.       TabIndex        =   7
  79.       Top             =   1980
  80.       Width           =   2955
  81.    End
  82.    Begin VB.TextBox txtFieldName 
  83.       Height          =   285
  84.       Left            =   1800
  85.       TabIndex        =   3
  86.       Top             =   840
  87.       Width           =   2535
  88.    End
  89.    Begin VB.TextBox txtTableName 
  90.       Height          =   285
  91.       Left            =   1800
  92.       TabIndex        =   1
  93.       Top             =   420
  94.       Width           =   2535
  95.    End
  96.    Begin VB.Label Label4 
  97.       AutoSize        =   -1  'True
  98.       BackColor       =   &H00C0C0C0&
  99.       Caption         =   "Field Li&st:"
  100.       Height          =   195
  101.       Left            =   300
  102.       TabIndex        =   6
  103.       Top             =   1980
  104.       Width           =   840
  105.    End
  106.    Begin VB.Label Label3 
  107.       AutoSize        =   -1  'True
  108.       BackColor       =   &H00C0C0C0&
  109.       Caption         =   "Field T&ype:"
  110.       Height          =   195
  111.       Left            =   300
  112.       TabIndex        =   4
  113.       Top             =   1320
  114.       Width           =   960
  115.    End
  116.    Begin VB.Label Label2 
  117.       AutoSize        =   -1  'True
  118.       BackColor       =   &H00C0C0C0&
  119.       Caption         =   "&Field Name:"
  120.       Height          =   195
  121.       Left            =   300
  122.       TabIndex        =   2
  123.       Top             =   900
  124.       Width           =   1020
  125.    End
  126.    Begin VB.Label Label1 
  127.       AutoSize        =   -1  'True
  128.       BackColor       =   &H00C0C0C0&
  129.       Caption         =   "&Table Name:"
  130.       Height          =   195
  131.       Left            =   300
  132.       TabIndex        =   0
  133.       Top             =   480
  134.       Width           =   1095
  135.    End
  136. End
  137. Attribute VB_Name = "frmMain"
  138. Attribute VB_Creatable = False
  139. Attribute VB_Exposed = False
  140. Option Explicit
  141.  
  142. Private IllegalCharacters(1 To 5) As String * 1
  143.  
  144. Const FIELDNAME = 1
  145. Const TABLENAME = 2
  146.  
  147. Private Sub Form_Load()
  148.  
  149.     ' Initialize the combo box and array of illegal characters.
  150.     FillTypeList
  151.     FillIllegalCharacterArray
  152.  
  153. End Sub
  154.  
  155. Sub FillTypeList()
  156.  
  157.     ' Add each field type to the list.
  158.     cboFieldTypes.AddItem "Counter"
  159.     cboFieldTypes.AddItem "Currency"
  160.     cboFieldTypes.AddItem "Date/Time"
  161.     cboFieldTypes.AddItem "Memo"
  162.     cboFieldTypes.AddItem "Number: Byte"
  163.     cboFieldTypes.AddItem "Number: Integer"
  164.     cboFieldTypes.AddItem "Number: Long"
  165.     cboFieldTypes.AddItem "Number: Single"
  166.     cboFieldTypes.AddItem "Number: Double"
  167.     cboFieldTypes.AddItem "OLE Object"
  168.     cboFieldTypes.AddItem "Text"
  169.     cboFieldTypes.AddItem "Yes/No"
  170.  
  171. End Sub
  172.  
  173. Sub FillIllegalCharacterArray()
  174.  
  175.     ' Fill the array with the list of characters that are illegal in
  176.     ' table names and field names.
  177.     IllegalCharacters(1) = "["
  178.     IllegalCharacters(2) = "]"
  179.     IllegalCharacters(3) = "."
  180.     IllegalCharacters(4) = "!"
  181.     IllegalCharacters(5) = "`"
  182.  
  183. End Sub
  184.  
  185. Private Sub cmdListTables_Click()
  186.  
  187.     ' Display the table list form modally.
  188.     frmTableList.Show 1
  189.  
  190. End Sub
  191.  
  192. Private Sub cmdAddField_Click()
  193.     Dim fieldType As String
  194.     
  195.     ' Make sure the name entered in txtFieldName meets all the
  196.     ' requirements for a legal field name.
  197.     If LegalName(FIELDNAME) Then
  198.  
  199.         ' Make sure that the user has selected a field type.
  200.         If cboFieldTypes.ListIndex > -1 Then
  201.  
  202.             ' Convert the field type selected by the user to the name
  203.             ' required by the CREATE TABLE syntax.
  204.             Select Case cboFieldTypes.TEXT
  205.                 Case "Counter"
  206.                     fieldType = "COUNTER"
  207.                 Case "Currency"
  208.                     fieldType = "CURRENCY"
  209.                 Case "Date/Time"
  210.                     fieldType = "DATETIME"
  211.                 Case "Memo"
  212.                     fieldType = "LONGTEXT"
  213.                 Case "Number: Byte"
  214.                     fieldType = "BYTE"
  215.                 Case "Number: Integer"
  216.                     fieldType = "SHORT"
  217.                 Case "Number: Long"
  218.                     fieldType = "LONG"
  219.                 Case "Number: Single"
  220.                     fieldType = "SINGLE"
  221.                 Case "Number: Double"
  222.                     fieldType = "DOUBLE"
  223.                 Case "OLE Object"
  224.                     fieldType = "LONGBINARY"
  225.                 Case "Text"
  226.                     fieldType = "TEXT"
  227.                 Case "Yes/No"
  228.                     fieldType = "BIT"
  229.             End Select
  230.  
  231.             ' Delimit the field name by [], then add the field name and
  232.             ' field type to the field list.
  233.             lstFields.AddItem "[" & txtFieldName & "] " & fieldType
  234.  
  235.             ' Reinitialize the field name text box and field type list
  236.             ' for entry of the next field.
  237.             txtFieldName = ""
  238.             cboFieldTypes.ListIndex = -1
  239.  
  240.         Else
  241.  
  242.             MsgBox "You must select a field type.", vbExclamation
  243.  
  244.         End If
  245.  
  246.     End If
  247.  
  248. End Sub
  249.  
  250. Function LegalName(whichName As Integer) As Boolean
  251.     Dim i As Integer
  252.     Dim isOK As Boolean
  253.     Dim nm As String
  254.     Dim db As DATABASE
  255.     Dim dbName As String
  256.     Dim td As TableDef
  257.  
  258.     On Error GoTo IllegalName
  259.  
  260.     ' Set the nm variable to the current contents of txtFieldName
  261.     ' or txtTableName, depending on which name is being checked.
  262.     If whichName = FIELDNAME Then
  263.         nm = txtFieldName
  264.     Else
  265.         nm = txtTableName
  266.     End If
  267.  
  268.     ' If the user has entered no name, generate an error.
  269.     If Len(nm) = 0 Then Error 32767
  270.  
  271.     ' If the name has a leading space, generate an error.
  272.     If Left$(nm, 1) = " " Then Error 32766
  273.  
  274.     ' If the name contains an illegal character, generate an error.
  275.     For i = 1 To 5
  276.         If InStr(nm, IllegalCharacters(i)) > 0 Then Error 32765
  277.     Next i
  278.  
  279.     ' If the name contains an ANSI control character (ANSI codes
  280.     ' 0 to 31), generate an error.
  281.     For i = 0 To 31
  282.         If InStr(nm, Chr(i)) > 0 Then Error 32764
  283.     Next i
  284.  
  285.     ' If the name being checked is a field name and the field name
  286.     ' has already been used, generate an error.
  287.     If whichName = FIELDNAME Then
  288.         For i = 0 To lstFields.ListCount - 1
  289.             If nm = lstFields.List(i) Then Error 32763
  290.         Next i
  291.  
  292.     ' If the name being checked is a table name and the table name
  293.     ' already exists in DATABASE_NAME, generate an error.
  294.     ElseIf whichName = TABLENAME Then
  295.     ' Get the database name and open the database.
  296.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  297.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  298.     For Each td In db.TableDefs
  299.             If td.Name = nm Then Error 32762
  300.         Next
  301.     End If
  302.  
  303.     ' No error was generated, so the name must be legal.
  304.     LegalName = True
  305.  
  306. Exit Function
  307.  
  308. IllegalName:
  309.     Dim errorMsg As String
  310.     Dim context As String
  311.  
  312.     ' Set the context depending on the type of name being checked.
  313.     ' The context is used in the error messages.
  314.     If whichName = FIELDNAME Then
  315.         context = "field name"
  316.     Else
  317.         context = "table name"
  318.     End If
  319.  
  320.     ' Build an error message based on the user-defined error that occurred.
  321.     Select Case Err
  322.         Case 32767
  323.             errorMsg = "You must enter a " & context & "."
  324.         Case 32766
  325.             errorMsg = "The " & context & " cannot begin with a space."
  326.         Case 32765
  327.             errorMsg = "The " & context & " contains the illegal character "
  328.             errorMsg = errorMsg & IllegalCharacters(i) & "."
  329.         Case 32764
  330.             errorMsg = "The " & context & " contains the control character "
  331.             errorMsg = errorMsg & "with the ANSI value" & Str$(i) & "."
  332.         Case 32763
  333.             errorMsg = "The field name " & nm
  334.             errorMsg = errorMsg & " already exists in the field name list."
  335.         Case 32762
  336.             errorMsg = "The table name " & nm
  337.             errorMsg = errorMsg & " already exists in the database "
  338.             errorMsg = errorMsg & dbName & "."
  339.         Case Else
  340.             ' Visual Basic's default error message.
  341.             errorMsg = Error$
  342.     End Select
  343.  
  344.     ' Display the error message.
  345.     MsgBox errorMsg, vbExclamation
  346.  
  347.     ' Return false to indicate that the name being checked was not legal.
  348.     LegalName = False
  349.  
  350. Exit Function
  351.  
  352. End Function
  353.  
  354. Private Sub cmdRemoveField_Click()
  355.  
  356.     ' If the user has selected a field, remove it from the list.
  357.     ' Otherwise, just ignore the click.
  358.     If lstFields.ListIndex > -1 Then lstFields.RemoveItem lstFields.ListIndex
  359.  
  360. End Sub
  361.  
  362. Private Sub cmdCreateTable_Click()
  363.     Dim sql As String
  364.     Dim fieldList As String
  365.     Dim i As Integer
  366.     Dim db As DATABASE
  367.     Dim dbName As String
  368.  
  369.     On Error GoTo CreateTableError
  370.  
  371.     Screen.MousePointer = 11
  372.     
  373.     ' Make sure the name entered in txtTableName meets all the
  374.     ' requirements for a legal table name.
  375.     If LegalName(TABLENAME) Then
  376.  
  377.         ' Make sure the user has created at least one field.
  378.         If lstFields.ListCount > 0 Then
  379.  
  380.             ' Build the list of fields that will be used as an argument in
  381.             ' the CREATE TABLE statement.
  382.             fieldList = " (" & lstFields.List(0)
  383.             For i = 1 To lstFields.ListCount - 1
  384.                 fieldList = fieldList & ", " & lstFields.List(i)
  385.             Next i
  386.             fieldList = fieldList & ") "
  387.  
  388.             ' Build the SQL statement.
  389.             sql = "CREATE TABLE [" & txtTableName & "]" & fieldList
  390.  
  391.   ' Get the database name and open the database.
  392.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  393.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  394.     
  395.     db.Execute (sql)
  396.             
  397.             Screen.MousePointer = 0
  398.             MsgBox "Table created successfully."
  399.  
  400.             ' Initialize txtTableName and the fields list for the next table.
  401.             txtTableName = ""
  402.             lstFields.Clear
  403.  
  404.         Else
  405.  
  406.             MsgBox "You must define at least one field.", vbExclamation
  407.  
  408.         End If
  409.  
  410.     End If
  411.  
  412. Exit Sub
  413. CreateTableError:
  414.     Screen.MousePointer = 0
  415.     MsgBox Error$, vbExclamation
  416. Exit Sub
  417. End Sub
  418.  
  419. Private Sub cmdClose_Click()
  420.     Dim errorMsg As String
  421.  
  422.     ' If the user has entered a partial table definition, make sure that the
  423.     ' user wants to abandon it. If so, end the program.
  424.     If txtTableName <> "" Or lstFields.ListCount > 0 Then
  425.         errorMsg = "Do you want to abandon operations on the current table?"
  426.         If MsgBox(errorMsg, vbQuestion + vbYesNo + vbDefaultButton2) = vbYes _
  427.             Then End
  428.  
  429.     Else
  430.  
  431.         ' No partial table definition, so just end the program
  432.         End
  433.  
  434.     End If
  435.  
  436. End Sub
  437.  
  438.  
  439.